package com.toolServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import com.mysql.mysqlConnect;
import com.dom.*;
import com.function.createID;

public class postInfo extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
            处理申请报灾的post请求
        */
        // 初始化数据库
        mysqlConnect conn = new mysqlConnect();
        // 获取ID
        createID create = new createID();
        // 初始化事件对象
        info temp = new info();
        // 获取灾情地点，解决中文传输的乱码问题
        String location = new String(request.getParameter("location").getBytes("ISO8859_1"), "UTF-8");
        // 获取申报人姓名
        String name = (String) request.getSession().getAttribute("username");
        System.out.println(location+"-"+name);
        // 设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 存入对象
        temp.setId(create.getID());
        temp.setLocation(location);
        temp.setLon(Float.parseFloat(request.getParameter("longitude")));
        temp.setLat(Float.parseFloat(request.getParameter("latitude")));
        temp.setName(name);
        // 时间数据的转化
        try
        {
            temp.setDate(format.parse(request.getParameter("time")));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // 获取次生灾害
        String type = request.getParameter("type");
        if (type.equals("earthquake"))
        {
            type = "地震";
        }
        else if (type.equals("no"))
        {
            type = "无";
        }
        else if (type.equals("waterflood"))
        {
            type = "洪水";
        }
        else if (type.equals("mud"))
        {
            type = "泥石流";
        }
        else if (type.equals("fire"))
        {
            type = "大火";
        }
        // 存入对象
        temp.setType(type);
        // 获取震感，存入对象
        String degree = request.getParameter("degree");
        if (degree.equals("no"))
        {
            degree = "无震感";
        }
        else if (degree.equals("light"))
        {
            degree = "灯具轻微晃动";
        }
        else if (degree.equals("door"))
        {
            degree = "门窗轻微作响";
        }
        else if (degree.equals("inside"))
        {
            degree = "室内人有明显震感";
        }
        else if (degree.equals("outside"))
        {
            degree = "是外人有明显震感";
        }
        else if (degree.equals("furniture"))
        {
            degree = "家具晃动";
        }
        else if (degree.equals("people"))
        {
            degree = "站立不稳";
        }
        temp.setDegree((degree));
        // 获取民众反应，存入对象
        String reaction = request.getParameter("reaction");
        if (reaction.equals("rare"))
        {
            reaction = "极个别在户外避震";
        }
        else if (reaction.equals("some"))
        {
            reaction = "少数在户外避震";
        }
        else if (reaction.equals("more"))
        {
            reaction = "多数在户外避震";
        }
        else if (reaction.equals("most"))
        {
            reaction = "均在在户外避震";
        }
        temp.setReaction(reaction);
        // 获取建筑物破坏程度，存入对象
        temp.setDestory(Float.parseFloat(request.getParameter("destory")));
        // 获取死亡人数，存入对象
        temp.setDead(Integer.parseInt(request.getParameter("dead")));
        // 获取受伤人数，存入对象
        temp.setHurt(Integer.parseInt(request.getParameter("hurt")));
        /* 基础设施灾情信息 */
        // 电力系统
        String elec = request.getParameter("elec");
        if (elec.equals("normal"))
        {
            elec = "供电正常";
        }
        else if (elec.equals("abnormal"))
        {
            elec = "局部停电";
        }
        else if (elec.equals("broken"))
        {
            elec = "大范围停电";
        }
        temp.setElec(elec);
        // 通信系统
        String concat = request.getParameter("concat");
        if (concat.equals("normal"))
        {
            concat = "通信正常";
        }
        else if (concat.equals("abnormal"))
        {
            concat = "局部中断";
        }
        else if (concat.equals("broken"))
        {
            concat = "大范围中断";
        }
        temp.setConcat(concat);
        // 交通系统
        String traffic = request.getParameter("traffic");
        if (traffic.equals("normal"))
        {
            traffic = "通行正常";
        }
        else if (traffic.equals("abnormal"))
        {
            traffic = "局部中断";
        }
        else if (traffic.equals("broken"))
        {
            traffic = "大范围中断";
        }
        temp.setTraffic(traffic);
        // 供水系统
        String water = request.getParameter("water");
        if (water.equals("normal"))
        {
            water = "供水正常";
        }
        else if (water.equals("abnormal"))
        {
            water = "局部停水";
        }
        else if (water.equals("broken"))
        {
            water = "大范围停水";
        }
        temp.setWater(water);
        // 供气系统
        String gas = request.getParameter("gas");
        if (gas.equals("normal"))
        {
            gas = "供气正常";
        }
        else if (gas.equals("abnormal"))
        {
            gas = "局部停气";
        }
        else if (gas.equals("broken"))
        {
            gas = "大范围停气";
        }
        temp.setGas(gas);
        // 救灾物资需求度
        String aid = String.format("食物:%s人/天;帐篷:%s顶;棉被:%s床;药品:%s",
                request.getParameter("food"), request.getParameter("tent"), request.getParameter("quilt"),
                new String(request.getParameter("drug").getBytes("ISO8859_1"), "UTF-8"));
        temp.setAid(aid);
        System.out.println(temp.toString());
        // 将灾害事件信息同布到数据库中
        // 此处的插入为待审核的事件，主页暂时不会显示该事件信息
        conn.insertTempInfo(temp);
        // 返回到主页
        response.sendRedirect("home");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
            接受get请求，返回jsp页面
         */
        response.sendRedirect("postInfo.jsp");
    }

}
